<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/19
 * Time: 18:03
 */
namespace app\api\controller;
use think\Db;
class My extends Base {
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }
    /**
     * 获取个人信息
     * @return \think\response\Json
     */
    public function userInfo(){
        extract(input());
        $user = $this->getUserInfo($user_id);
        $user['sheng_name'] = '';
        $user['shi_name'] = '';

        if($user['user_verify'] == 1){
            $user['user_sheng'] = $user['user_sheng2'];
            $user['user_shi'] = $user['user_shi2'];
            $user['user_hospital'] = $user['user_hospital2'];
            $user['user_room'] = $user['user_room2'];
            $user['user_job_desc'] = $user['user_job_desc2'];
        }

        if($user['user_sheng'] && $user['user_shi']){
            $area = [$user['user_sheng'],$user['user_shi']];
            $ss = Db::table('up_area')
                ->where('district_id','in',$area)
                ->column('district');
            $user['sheng_name'] = $ss[0];
            $user['shi_name'] = $ss[1];
        }
        return json(['status'=>200,'msg'=>$user]);

    }
    /**
     * 获取个人信息
     * @return \think\response\Json
     */
    public function seeUserInfo(){
        extract(input());
        $field = 'user_username,user_id,user_image,user_phone,user_sex,user_hospital,user_room,user_job_desc,user_type,user_shi';
        $user = $this->getUserInfo($uid,$field,2);
        if($user['user_type'] == 1){
            $shi = $user['user_shi'];
        }else{
            $c = Db::table('up_company')
                ->where('c_id',$user['user_hospital'])
                ->field('c_shi,c_name')
                ->find();
            $ct = Db::table('up_company_team')
                ->where('ct_id',$user['user_room'])
                ->value('ct_name');
            $user['user_hospital'] = $c['c_name'];
            $user['user_room'] = $ct;
            $shi = $c['c_shi'];
        }
        $user['shi'] = Db::table('up_area')
            ->where('district_id',$shi)
            ->value('district');
        $friend = Db::table('up_friend')
            ->where('friend_user_id',$user_id)
            ->where('friend_other_user',$uid)
            ->find();
        $user['friend_key'] = 1;
        $user['friend_id'] = 0;
        if($friend){
            $user['friend_key'] = $friend['friend_type'] + 1;
            $user['friend_id'] = $friend['friend_id'];
        }
        return json(array('status'=>200,'msg'=>$user));

    }
    /**
     * 修改个人信息（医生和业务员）
     * @return \think\response\Json
     */
    public function userInfoEdit(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_hospital,user_room,user_sheng,user_shi,user_job_desc',2);
        $today = date('Y-m-d H:i:s');
        if($user['user_type'] != 1){
            $data = array();
            $data['user_updatetime'] = $today;
            $data['user_image'] = $user_image;
            $data['user_username'] = $user_username;
            $data['user_sex'] = $user_sex;
            $data['user_birthday'] = $user_birthday;
            $data['user_job_desc'] = $user_job;
        }else {
            $data = array();
            $data['user_updatetime'] = $today;
            $data['user_image'] = $user_image;
            $data['user_username'] = $user_username;
            $data['user_sex'] = $user_sex;
            $data['user_edu'] = $user_edu;
            $data['user_birthday'] = $user_birthday;
            $data['user_card'] = $user_card;
            //
            if ($user['user_hospital'] != $user_hospital || $user['user_room'] != $user_room || $user['user_job_desc'] != $user_job || $user['user_sheng'] != $user_sheng || $user['user_shi'] != $user_shi) {
                $data['user_verify'] = 1;
                //-------------------------------------------
                $data['user_sheng2'] = $user_sheng;
                $data['user_shi2'] = $user_shi;
                $data['user_hospital2'] = $user_hospital;
                $data['user_room2'] = $user_room;
                $data['user_job_desc2'] = $user_job;
            }
//            else{
//                $data['user_sheng'] = $user_sheng;
//                $data['user_shi'] = $user_shi;
//                $data['user_hospital'] = $user_hospital;
//                $data['user_room'] = $user_room;
//                $data['user_job_desc'] = $user_job;
//            }
        }
        $where['user_id'] = $user_id;
        $res = Db::table('up_u_user')->where($where)->update($data);
        if($res){
            return json(array('status'=>200,'msg'=>"修改成功"));
        }else{
            return json(array('status'=>0,'msg'=>"修改失败"));
        }

    }

    /**
     * 注销
     * @return \think\response\Json
     */
    public function userCancel()
    {
        $user_id = input('user_id');
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        if($phone == '') {
            return json(['status' => 0, 'msg' => '请输入手机号码']);
        }
        if($code == '') {
            return json(['status' => 0, 'msg' => '请输入验证码']);
        }
        if(!preg_match("/^1[123456789]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 601) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        $res = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->setField('user_status',2);
        if($res){
            return json(['status' => 200, 'msg' => '注销成功！']);
        }else{
            return json(['status' => 0, 'msg' => '注销失败，请稍后重试！']);
        }
    }
    /**
     * 我的同事（业务员专用）
     * @return \think\response\Json
     */
    public function myColleague(){
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_hospital,user_room',2);
        $where = [
            'user_hospital' => $user['user_hospital'],
            'user_id' => ['<>',$user_id],
            'user_type' => 2
        ];
        $field = 'user_username,user_id,user_image,user_hospital,user_room,user_job_desc,user_type';
        $field .= ',c_name,ct_name';
        $data = Db::table('up_u_user')->alias('u')
            ->join('up_company c','u.user_hospital = c.c_id','left')
            ->join('up_company_team ct','u.user_room = ct.ct_id','left')
            ->where($where)
            ->order('user_auth desc')
            ->page($page,20)
            ->field($field)
            ->select();
        return json(array('status'=>200,'msg'=>$data));
    }
    /**
     * 我的项目
     * pro_type:1未开始  2进行中  已结束
     * type:1是待审核  2是同意  3是拒绝
     * @return \think\response\Json
     */
    public function myProject(){
        extract(input());
        $where = [
            'pu_user_id' => $user_id
        ];
        if($pro_type != 0){
            $where['pro_status'] = $pro_type;
        }
        if($type != 0){
            $where['pu_status'] = $type;
        }
        $field = 'pro_id,pro_name,pro_start_time,pro_end_time,pro_time,pro_status';
        $field .= ',pu_id,pu_status,pu_remark';
        $field .= ',user_id,user_username,user_image';
        $data = Db::table('up_project_user pu')
            ->join('up_project p','pu.pu_pro_id = p.pro_id','left')
            ->join('up_u_user u','p.pro_user_id = u.user_id','left')
            ->where($where)
            ->order('pro_update desc')
            ->page($page,10)
            ->field($field)
            ->select();
        return json(array('status'=>200,'msg'=>$data));
    }
    /**
     * 我的收藏
     * type: 0是全部 1是资料  2是项目  3是会议
     * @return \think\response\Json
     */
    public function myLikes(){
        extract(input());
        $where = [
            'lpro_user_id' => $user_id
        ];
        if($type != 0){
            $where['lpro_type'] = $type;
        }
        $data = Db::table('up_like_project')
            ->where($where)
            ->order('lpro_time desc')
            ->page($page,10)
            ->field('lpro_id,lpro_type,lpro_prom_id,lpro_edit,lpro_time')
            ->select();
        $res = [];
        if($data){
            $user = $this->getUserInfo($user_id,'user_type,user_auth,user_hospital,user_sheng,user_shi',2);
            foreach ($data as $k => $v){
                if($v['lpro_type'] == 1){
                    $one = Db::table('up_company_product_data cpd')
                        ->join('up_company_product cp','cpd.cpd_cp_id=cp.cp_id','LEFT')
                        ->join('up_u_user u','cp.cp_charge_uid=u.user_id','LEFT')
                        ->where('cpd_id',$v['lpro_prom_id'])
                        ->field('cpd.*,cp_id,cp_c_id,user_id,user_username,user_image')
                        ->find();
                    $realdata = $this->productRealCharger($one['cp_id'],$user_id,$one['cp_c_id']);
                    $one['user_id'] = '';
                    $one['user_username'] = '';
                    $one['user_image'] = '';
                    if($realdata){
                        $one['user_id'] = $realdata['user_id'];
                        $one['user_username'] = $realdata['user_username'];
                        $one['user_image'] = $realdata['user_image'];
                    }
                }else if($v['lpro_type'] == 2){
                    $one = Db::table('up_project p')
                        ->join('up_company c','p.pro_c_id=c.c_id','LEFT')
                        ->join('up_u_user u','p.pro_user_id=u.user_id','LEFT')
                        ->where('pro_id',$v['lpro_prom_id'])
                        ->field('p.*,user_id,user_username,user_image')
                        ->find();
                }else{
                    $one = Db::table('up_meeting m')
                        ->join('up_u_user u','m.m_user_id=u.user_id','LEFT')
                        ->where('m_id',$v['lpro_prom_id'])
                        ->field('m.*,user_id,user_username,user_image')
                        ->find();
                    //会议是否参加
                    $join = Db::table('up_meeting_user')
                        ->where('mu_user_id', $user_id)
                        ->where('mu_m_id', $v['lpro_prom_id'])
                        ->field('mu_status')
                        ->find();
                    $one['join'] = 0; //未参加
                    if($join){
                        $one['join'] = $join['mu_status']; //1是待审核  2是同意  3是拒绝
                    }
                }
                $one['type'] = $v['lpro_type'];
                $one['lpro_id'] = $v['lpro_id'];
                $one['lpro_edit'] = $v['lpro_edit'];
                $res[] = $one;
            }
        }
        return json(array('status'=>200,'msg'=>$res));
    }
    /**
     * 我的收藏——取消更新
     * @return \think\response\Json
     */
    public function myLikesEdit(){
        extract(input());
        $where = [
            'lpro_id' => $lpro_id
        ];
        $lpro_edit = Db::table('up_like_project')
            ->where($where)
            ->value('lpro_edit');
        if($lpro_edit == 2){
            $res = Db::table('up_like_project')
                ->where($where)
                ->setField('lpro_edit',1);
            if($res){
                return json(array('status'=>200,'msg'=>'ok'));
            }else{
                return json(array('status'=>10,'msg'=>'error'));
            }
        }
        return json(array('status'=>200,'msg'=>'ok'));
    }
    /**
     * 常见问题
     * @return \think\response\Json
     */
    public function questions(){
        extract(input());
        $list = Db::table('up_question')
            ->field('q_content',true)
            ->page($page,20)
            ->order('q_time desc')
            ->select();
        return json(['status'=>200,'msg'=>$list]);

    }
    /**
     * 常见问题详情
     * @return \think\response\Json
     */
    public function questionInfo(){
        extract(input());
        $where['q_id'] = $q_id;
        $list = Db::table('up_question')
            ->where($where)
            ->find();
        return json(['status'=>200,'msg'=>$list]);

    }
    public function feedback(){
            extract(input());
        $data = [
            'f_user_id' => $user_id,
            'feedback_content' => $content,
            'feedback_phone' => $phone,
            'feedback_time' => date('Y-m-d H:i:s'),
        ];
        $res = Db::table('up_feedback')->insert($data);
        if($res){
            return json(array('status'=>200,'msg'=>"成功"));
        }else{
            return json(array('status'=>0,'msg'=>"失败"));
        }
    }
    //--------------------------------------签到相关--------------------------------------
    /**
     * 获取我的签到记录
     * @return \think\response\Json
     */
    public function getMyDates(){
        extract(input());
        if(!isset($year) || !$year){
            $year = date('Y');
        }
        if(!isset($month) || !$month){
            $month = (int)date('m');
        }
        $res = Db::table('up_user_date')
            ->where('ud_user_id',$user_id)
            ->where('ud_year',$year)
            ->where('ud_month',$month)
            ->order('ud_date asc')
            ->column('ud_date');
        if($res){
            foreach ($res as $k => $v){
                $res[$k] = substr($v,8,2);
            }
        }
        return json(array('status'=>200,'msg'=>$res));
    }
    /**
     * 签到
     * @return \think\response\Json
     */
    public function signToday(){
        extract(input());
        $year = date('Y');
        $month = (int)date('m');
        $today = date('Y-m-d');
        $have = Db::table('up_user_date')
            ->where('ud_user_id',$user_id)
            ->where('ud_date',$today)
            ->find();
        if($have){
            return json(array('status'=>0,'msg'=>"今天已经签到过了"));
        }else{
            $get_in = Db::table('up_about')->where('id',7)->value('content');
            $data = [
                'ud_user_id' => $user_id ,
                'ud_year' => $year ,
                'ud_month' => $month ,
                'ud_date' => $today ,
                'ud_time' => date('Y-m-d H:i:s') ,
            ];
            $res = Db::table('up_user_date')->insert($data);
            if($res){
                Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_integral',$get_in);
                allInRevenue(1,$user_id,$get_in,0);
                return json(array('status'=>200,'msg'=>"成功",'num'=>$get_in));
            }else{
                return json(array('status'=>0,'msg'=>"失败"));
            }
        }
    }
    /**
     * 获取我的积分获取兑换记录
     * @return \think\response\Json
     */
    public function myRecode(){
        extract(input());
        $res = Db::table('up_user_revenue')
            ->where('revenue_user_id',$user_id)
            ->order('revenue_time asc')
            ->page($page,20)
            ->select();
        return json(array('status'=>200,'msg'=>$res));
    }
    /**
     * 兑换网盘空间
     * $num：兑换多少M
     * @return \think\response\Json
     */
    public function addMyRoom(){
        extract(input());
        $need_ji = Db::table('up_about')->where('id',8)->value('content');//一M需要多少积分
        $user_integral = $this->getUserInfo($user_id,'user_integral');
        $num = (int)$num;
        if($num <= 0){
            return json(array('status'=>100,'msg'=>'兑换不能小于0'));
        }
        $need = $num * $need_ji;
        if($user_integral < $need){
            return json(array('status'=>100,'msg'=>'您的积分不足'));
        }
        $res = Db::table('up_u_user')->where('user_id',$user_id)
            ->inc('user_ram',$num*1024)->dec('user_integral',$need)
            ->update();
        if($res){
            allInRevenue(2,$user_id,$need,$num);
            return json(array('status'=>200,'msg'=>'兑换成功'));
        }else{
            return json(array('status'=>100,'msg'=>'兑换失败，请刷新重试'));
        }

    }
}